package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.CollectMapper;
import com.example.hospitalsystem.mapper.DesignMapper;
import com.example.hospitalsystem.pojo.entity.Collects;
import com.example.hospitalsystem.pojo.entity.Design;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.CollectService;
import com.example.hospitalsystem.utils.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collects> implements CollectService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private DesignMapper designMapper;

    @Override
    public Result<Object> collectDesign(int designId) {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);
        Collects collects=new Collects();
        LocalDateTime time=LocalDateTime.now();
        collects.setCollectTime(time);
        collects.setDesignId(designId);
        collects.setUserId(userId);
        this.save(collects);
        return Result.build(collects,200,"关注成功");
    }

    @Override
    public Result<Object> cancleCollectDesign(int designId) {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);
        LambdaQueryWrapper<Collects> collectsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        collectsLambdaQueryWrapper.eq(Collects::getDesignId,designId)
                .eq(Collects::getUserId,userId);
        int i=baseMapper.delete(collectsLambdaQueryWrapper);
        if(i<0){
            return Result.build("取消关注失败");
        }
        return Result.build("取消关注成功");
    }

    @Override
    public Result<Object> lookAllCollect() {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);
        LambdaQueryWrapper<Collects> collectsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        collectsLambdaQueryWrapper.eq(Collects::getUserId,userId);

        List<Collects> collectsList=baseMapper.selectList(collectsLambdaQueryWrapper);

        List<Design> designList=new ArrayList<>();
        for(Collects collects:collectsList){
            Design design=new Design();
            LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
            designLambdaQueryWrapper.eq(Design::getDesignId,collects.getDesignId());
            design=designMapper.selectOne(designLambdaQueryWrapper);
            if(design!=null){
                designList.add(design);
            }
        }
        return Result.build(designList,200,"返回所有收藏信息成功");
    }

    @Override
    public Result<Object> getStateCollect(Integer designId) {
        LambdaQueryWrapper<Collects> collectsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        collectsLambdaQueryWrapper.eq(Collects::getDesignId,designId);
        Collects collects=baseMapper.selectOne(collectsLambdaQueryWrapper,false);

        if(collects!=null){
            return Result.build(1,200,"已收藏");
        }
        return Result.build(0,200,"未收藏");
    }
}
